package cn.xopencode.appletweb.controller.record.pdroom;

import cn.xopencode.common.framework.util.StringUtils;
import cn.xopencode.common.framework.vo.CommonResult;
import cn.xopencode.common.framework.vo.PageResult;
import cn.xopencode.recordservice.rpc.pdroom.PowerDistributionRoomRpc;
import cn.xopencode.recordservice.rpc.pdroom.dto.PowerDistributionRoomCreateDTO;
import cn.xopencode.recordservice.rpc.pdroom.dto.PowerDistributionRoomPageDTO;
import cn.xopencode.recordservice.rpc.pdroom.dto.PowerDistributionRoomQueryDTO;
import cn.xopencode.recordservice.rpc.pdroom.dto.PowerDistributionRoomUpdateDTO;
import cn.xopencode.recordservice.rpc.pdroom.vo.PdRoomDeptRelVO;
import cn.xopencode.recordservice.rpc.pdroom.vo.PowerDistributionRoomPageVO;
import cn.xopencode.recordservice.rpc.pdroom.vo.PowerDistributionRoomUpdateVO;
import cn.xopencode.recordservice.rpc.pdroom.vo.PowerDistributionRoomVO;
import cn.xopencode.systemservice.rpc.datadict.DataDictRpc;
import cn.xopencode.systemservice.rpc.datadict.vo.DataDictVO;
import cn.xopencode.userservice.rpc.dept.DeptRpc;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Api(tags = "配电室管理 API")
@RestController
@RequestMapping("/pdroom")
@Validated
public class PowerDistributionRoomController {

    @DubboReference(version = "${dubbo.consumer.PowerDistributionRoomRpc.version}")
    private PowerDistributionRoomRpc powerDistributionRoomRpc;

    @DubboReference(version = "${dubbo.consumer.DataDictRpc.version}")
    private DataDictRpc dataDictRpc;

    @DubboReference(version = "${dubbo.consumer.DeptRpc.version}")
    private DeptRpc deptRpc;

    private String pdroomAreaDict = "pdroom_area";
    private String recordTypeDict = "record_type";
    private String pdroomMajorDict = "pdroom_major";

    /**
     * 根据配电室名称模糊匹配所有的配电室
     * @param roomName
     * @return
     */
    @GetMapping("/getAllByRoomName")
    @ApiOperation("根据配电室名称模糊匹配所有的配电室")
    @ApiImplicitParam(name = "roomName", value = "配电室名称", required = false)
    public CommonResult<List<PowerDistributionRoomVO>> getPowerDistributionRoomVO(
            @RequestParam(name = "roomName", required = false) String roomName){
        CommonResult<List<PowerDistributionRoomVO>> list = powerDistributionRoomRpc.getPowerDistributionRoomList(roomName);
        return list;
    }

    /**
     * 根据查询条件 分页获取所有的配电室
     * @param pageDTO
     * @return
     */
    @PostMapping("/getAllByQuery")
    @ApiOperation("根据查询条件分页获取所有的配电室")
    public CommonResult<PageResult<PowerDistributionRoomPageVO>> getPagePowerDistributionRoom(PowerDistributionRoomPageDTO pageDTO){
        CommonResult<PageResult<PowerDistributionRoomPageVO>> result = powerDistributionRoomRpc.pagePowerDistributionRoom(pageDTO);
        List<PowerDistributionRoomPageVO> list = result.getData().getList();
        list.forEach(pageVO -> {
            if(!StringUtils.isEmpty(pageVO.getDeptNames())){
                pageVO.setDeptNames(deptRpc.getDeptNameList(pageVO.getDeptNames()).getData());
            }
        });
        return result;
    }

    /**
     *  新增配电室记录
     * @param roomDTO
     * @return
     */
    @PostMapping("/add")
    @ApiOperation("新增配电室记录")
    public CommonResult<Integer> createPowerDistributionRoom(@Valid PowerDistributionRoomCreateDTO roomDTO){
        return powerDistributionRoomRpc.createPowerDistributionRoom(roomDTO);
    }

    /**
     *  更新配电室记录
     * @param roomDTO
     * @return
     */
    @PostMapping("/update")
    @ApiOperation("更新配电室记录")
    public CommonResult<Integer> updatePowerDistributionRoom(@Valid PowerDistributionRoomUpdateDTO roomDTO){
        return powerDistributionRoomRpc.updatePowerDistributionRoom(roomDTO);
    }

    /**
     * 根据id获取配电室详细
     * @param roomId
     * @return
     */
    @GetMapping("/getById")
    @ApiOperation("根据id获取配电室详细")
    public CommonResult<PowerDistributionRoomUpdateVO> getPowerDistributionRoomById(String roomId){
        CommonResult<PowerDistributionRoomUpdateVO> result = powerDistributionRoomRpc.getPowerDistributionRoomById(roomId);
        PowerDistributionRoomUpdateVO updateVO = result.getData();
        if(updateVO != null){
            List<String> list = updateVO.getRelVOList().stream().map(PdRoomDeptRelVO::getDeptId).collect(Collectors.toList());
            String deptNames = deptRpc.getDeptNameList(StringUtils.join(list, ",")).getData();
            updateVO.setDeptNames(deptNames);
        }
        return result;
    }

    /**
     * 修改配电室是否可用的状态
     * @param roomId
     * @return
     */
    @PostMapping("/changeStatus")
    @ApiOperation("修改配电室是否可用的状态")
    public CommonResult<Integer> changeStatus(String roomId, Boolean flag){
        return powerDistributionRoomRpc.changeStatus(roomId, flag);
    }

    /**
     * 根据参数获取线路名称
     * @param
     * @return
     */
    @PostMapping("/getLineNameByParam")
    @ApiOperation("根据参数获取线路名称")
    public CommonResult<Map<String, String>> getLineNameByParam(PowerDistributionRoomQueryDTO queryDTO){
        return powerDistributionRoomRpc.getLineNameByParam(queryDTO);
    }

    /**
     * 根据巡检记录类型获取配电室名称
     * @param
     * @return
     */
    @PostMapping("/getPdroomNameByType")
    @ApiOperation("根据巡检记录类型获取配电室名称")
    public CommonResult<List<PowerDistributionRoomVO>> getPdroomNameListByType(Integer recordType){
        return powerDistributionRoomRpc.getPdroomNameListByType(recordType);
    }

    /**
     * 获取配电室所属片区字典信息
     * @return
     */
    @GetMapping("/getPdroomAreaDict")
    @ApiOperation("获取配电室所属片区字典信息")
    public CommonResult<List<DataDictVO>> getPowerDistributionRoomAreaDict(){
        return dataDictRpc.listDataDictsByEnumValue(pdroomAreaDict);
    }

    /**
     * 获取配电室中巡检记录类型信息
     * @return
     */
    @GetMapping("/getRecordTypeDict")
    @ApiOperation("获取配电室中巡检记录类型信息")
    public CommonResult<List<DataDictVO>> getRecordTypeDict(){
        return dataDictRpc.listDataDictsByEnumValue(recordTypeDict);
    }

    /**
     * 获取配电室专业字典信息
     * @return
     */
    @GetMapping("/getPdroomMajorDict")
    @ApiOperation("获取配电室专业字典信息")
    public CommonResult<List<DataDictVO>> getPdroomMajorDict(){
        return dataDictRpc.listDataDictsByEnumValue(pdroomMajorDict);
    }

}
